package cn.wolfcode.web.controller;

import cn.wolfcode.service.IEmployeeService;
import cn.wolfcode.utils.TokenManager;
import cn.wolfcode.vo.JsonResult;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class LoginController {

    private final IEmployeeService employeeService;

    public LoginController(IEmployeeService employeeService) {
        this.employeeService = employeeService;
    }

    @RequestMapping("/login")
    public JsonResult<String> login(String username, String password) {
        try {
            // 登录业务
            String token = employeeService.login(username, password);
            return JsonResult.success(token);
        } catch (Exception e) {
            return JsonResult.failed(400, e.getMessage());
        }
    }

    @RequestMapping("/logout")
    public JsonResult<?> logout(@RequestHeader(value = TokenManager.TOKEN_NAME, required = false) String token) {
        TokenManager.removeInfo(token);
        return JsonResult.success();
    }
}
